import java.util.Arrays;
import java.util.Stack;

public class TestSort {

    // 直接插入排序
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (tmp < array[j]) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    // 希尔排序
    public static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (tmp < array[j]) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j] = tmp;
        }
    }

    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shell(array, gap);
        }
        shell(array, 1);
    }


    // 选择排序
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[minIndex] > array[j]) {
                    minIndex = j;
                }
            }
            swap(array, i, minIndex);
        }
    }

    public static void heapSort(int[] array) {
        // 建大根堆
        createHeap(array);
        // 排序
        int end = array.length - 1;  // 下标
        while (end > 0) {
            swap(array, 0, end);
            shiftDown(array, 0, end);
            end--;
        }
    }

    private static void createHeap(int[] array) {
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
            // 从根往下调整  (向下调整)
            shiftDown(array, parent, array.length);

        }
    }

    private static void shiftDown(int[] array, int parent, int len) {
        int child = parent * 2 + 1;
        while (child < len) {
            if (child + 1 < len && array[child] < array[child + 1]) {
                child++;
            }
            if (array[parent] < array[child]) {
                swap(array, parent, child);
                parent = child;
                child = parent * 2 + 1;
            } else {
                break;
            }
        }
    }


    // 快速排序
    public static void quickSort(int[] array) {
        quick(array, 0, array.length - 1);
    }

    private static void quick(int[] array, int start, int end) {

        if (start >= end) {
            return;
        }

        // 当区间小于一定的值时,直接对区间数据进行直接插入排序
        if (end - start <= 100) {
            insertSort2(array, start, end);
        }

        int index = threeMid(array, start, end);
        swap(array, index, start);

        int pivot = partition(array, start, end);
        quick(array, start, pivot - 1);
        quick(array, pivot + 1, end);
    }

    private static int partition(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right) {
            // 在右边找比 tmp 小的元素
            while (right > left && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    // 三数取中法
    private static int threeMid(int[] array, int start, int end) {
        int mid = (start + end) >>> 1;
        if (array[start] < array[end]) {
            if (array[mid] > array[end]) {
                return end;
            } else if (array[mid] > array[start]) {
                return mid;
            } else {
                return start;
            }
        } else {
            if (array[mid] > array[start]) {
                return start;
            } else if (array[mid] > array[end]) {
                return mid;
            } else {
                return end;
            }
        }
    }

    private static void insertSort2(int[] array, int start, int end) {
        for (int i = start + 1; i <= end; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= start; j--) {
                if (tmp < array[j]) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    // 快速排序 (非递归)
    public static void quickSort2(int[] array){
        Stack<Integer> stack = new Stack<>();
        int start = 0;
        int end = array.length-1;
        int pivot = partition(array,start,end);

        // 基准右边有两个或以上元素 放入栈中
        if (pivot+1 < end) {
            stack.push(pivot+1);
            stack.push(end);
        }
        if (pivot-1 > start) {
            stack.push(start);
            stack.push(pivot-1);
        }

        while (!stack.isEmpty()){
            end = stack.pop();
            start = stack.pop();
            pivot = partition(array,start,end);

            if (pivot+1 < end) {
                stack.push(pivot+1);
                stack.push(end);
            }
            if (pivot-1 > start) {
                stack.push(start);
                stack.push(pivot-1);
            }
        }
    }

    // 归并排序
    public static void mergeSort(int[] array){
        mergeHelp(array,0,array.length-1);
    }

    private static void mergeHelp(int[] array, int left, int right) {
        if (left >= right){
            return;
        }
        int mid = (left + right) >>> 1;
        mergeHelp(array, left, mid);
        mergeHelp(array, mid + 1, right);
        // 合并
        merge(array,left,right,mid);
    }

    private static void merge(int[] array,int low,int high,int mid){
        int[] tmp = new int[high-low+1];
        int k = 0;
        int s1 = low;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = high;
        while (s1 <= e1 && s2 <= e2) {
            // 两个归并段都有数据
            if (array[s1] <= array[s2]){
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= e1) {
            tmp[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmp[k++] = array[s2++];
        }

        for (int i = 0; i < k; i++) {
            array[i+low] = tmp[i];
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 5, 2, 7, 9, 6};
        // insertSort(arr);
        //selectSort(arr);
       // quickSort(arr);
        quickSort2(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void swap(int[] array, int a, int b) {
        int tmp = array[a];
        array[a] = array[b];
        array[b] = tmp;
    }
}
